import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Product from '../components/Product.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/product',
    component: Product
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, path, next) => {
  if (to.path === '/login') return next()
  const isLogin = localStorage.isLogin
  if (!isLogin) {
    Vue.prototype.$message({
      message: '请登录',
      type: 'error',
      showClose: true
    })
    return next('/login')
  }
  next()
})
export default router
